<?php 
include('/application/libraries/aliyun-dysms-php-sdk/api_demo/SmsDemo.php'); //这个是你下面实例化的类
include('DesUtils.php');
class Jiekouapi extends CI_Controller {
	public function __construct(){
		parent::__construct();
		//header('Content-Type: application/json; charset=utf-8');
		$this->load->helper(array('form', 'url'));
		$this->load->model('Model_m');// 第三方 实例 增删改查 的模型
		// $this->load->model('Model_m');// 第三方 实例 增删改查 的模型
		$this->load->model('Device_m');// 第三方 实例 增删改查 的模型
		$this->load->model('Config_m');// 第三方 实例 增删改查 的模型
		$this->load->model('Usersweixin_m');// 第三方 实例 增删改查 的模型
		$this->load->model('Recharge_m');// 第三方 实例 增删改查 的模型
		$this->load->library('api/userapi_service');// 用户中间层 实例 增删改查 的模型
		$this->load->library('session');
		
	}
	public function RequestHttp($url){
        // $url = "https://api.shenjian.io/?appid=49799ab372fc4b4e6bb8e8ad273bdda3&coin=Ethereum&currency=USD";
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在
        $tmpInfo = curl_exec($curl);     //返回api的json对象
        //关闭URL请求
        curl_close($curl);
        return $tmpInfo;    //返回json对象
    }
    //天数的

    
	//查询是否到期-流量的
	public function ResFlow(){
		$dataDevice = $this->Device_m->select(array('state'=>1));
		foreach ($dataDevice as $key => $val) {
			$two = substr($val['device_sn'],0,2);
			if ($two == 14 || $two == 10) {
				$success[] = $this->ResFlow_state($val['device_sn'],$val['flow']);
			}else{
				$success[] = array('code'=>0,'msg'=>'该号段不正确','access_number'=>$val['device_sn']);
			}
		}
		//写入日志
		foreach ($success as $key => $val) {
			// $data = "".$arr.""; //接到get请求 E30E65952253975D7C83F9AE4C200D77
	        $file  = 'log.txt';//要写入文件的文件名（可以是任意文件名），如果文件不存在，将会创建一个
	        $content =date("Y/m/d h:i:sa").";备注=".$val['msg'].";设备号=".$val['access_number']."\r\n";
	        if($f  = file_put_contents($file, $content,FILE_APPEND)){// 这个函数支持版本(PHP 5) 
	            echo "写入成功。<br />";
	        }
			
		}

	}
	public function ResFlow_state($access_number,$flow){
		header("Content-type:text/html;charset=utf-8");
		// header("Content-type:text/xml;charset=utf-8");
		//具体接口参数需参照自管理门户在线文档
		// $access_number='8986111823803770399'; //物联网卡号(149或10649号段)
		$user_id="miaoyue";		//用户名
		$password="V934hoxZwdE2ZmWl";//密码
		$method = "queryPakage";  //接口名-套餐使用量查询
		$arr=array($access_number,$user_id,$password,$method);//加密数组，数组所需参数根据对应的接口文档

		//key值指密钥，由电信提供，每个客户都有对应的key值，key值平均分为三段如下：
	    $key1 = "Jp2";
	    $key2 = "SMp";
	    $key3 = "4Ja";
	    $des=new DesUtils(); //加密工具类实例化

	    $arrOrder=$des->naturalOrdering($arr); //加密数组元素排序
	    $sign = $des->strEnc($arrOrder,$key1,$key2,$key3);
	    $passwords = $des->strEnc($password,$key1,$key2,$key3);
	    $time = time();
		$url = "http://api.e114.xin/m2m_ec/query.do?user_id=".$user_id."&access_number=".$access_number."&sign=".$sign."&passWord=".$passwords."&method=queryPakage";
		// $xml = $this->RequestHttp($url);
		// print_r($xml);exit;
		$reader = new XMLReader(); //实例化XMLReader
		$reader->open($url); //获取xml
		$i=1;
		while ($reader->read()) {
		 if ($reader->nodeType == XMLReader::TEXT) { //判断node类型
		   $m = $i%3;
		   $arr[] = $reader->value;
		   $i++;
		 }
		}
		$already = $arr[5];//已使用
		$left 	 = $arr[6];//剩余
		$total	 = $arr[7];//总量

		if (intval($flow) > intval($already)) {
			//如果足够就填写已使用的，
			$dataWhere['device_sn'] = $access_number;
			$dataArr['user_flow'] = $already;
			$this->Device_m->update($dataWhere,$dataArr);
			//日入记录-查询
			$file  = './flowlogs/'.date('Y-m-d-H').'chaxun.txt';//要写入文件的文件名（可以是任意文件名），如果文件不存在，将会创建一个
	        $content = date("Y/m/d h:i:sa").";备注=流量足够;设备号=".$access_number.";总量=".$total."KB;剩余=".$left."KB;已使用=".$already."KB\r\n";
	        file_put_contents($file, $content,FILE_APPEND);
			return array('code'=>5,'msg'=>'流量足够','access_number'=>$access_number);
		}else{
			//如果不够就停用naturalOrdering
			return $this->Functionceshiguan($access_number);
		}

		//停用的接口
	}

	//关机
	public function Functionceshiguan($access_number)
	{
		header("Content-type:text/html;charset=utf-8");
		// $access_number=$access_number; //物联网卡号(149或10649号段)
		$user_id="miaoyue";		//用户名
		$password="V934hoxZwdE2ZmWl";//密码
		$method = "disabledNumber";  //接口名-套餐使用量查询
		$orderTypeId = 19;
		$arr=array($access_number,$user_id,$password,$method,$orderTypeId);//加密数组，数组所需参数根据对应的接口文档
	    $key1 = "Jp2";
	    $key2 = "SMp";
	    $key3 = "4Ja";
	    $des=new DesUtils(); //加密工具类实例化
		
		$arrOrder=$des->naturalOrdering($arr); //加密数组元素排序
    	$sign = $des->strEnc($arrOrder,$key1,$key2,$key3);
	    $passwords = $des->strEnc($password,$key1,$key2,$key3);

		$urls = "http://api.e114.xin/m2m_ec/query.do?user_id=".$user_id."&access_number=".$access_number."&sign=".$sign."&passWord=".$passwords."&method=".$method."&orderTypeId=".$orderTypeId."";

		$reader = new XMLReader(); //实例化XMLReader
		$reader->open($urls); //获取xml
		$i=1;
		while ($reader->read()) {
		 if ($reader->nodeType == XMLReader::TEXT) { //判断node类型
		   $m = $i%3;
		   $arrs[] = $reader->value;
		   $i++;
		 }
		}
		
		if ($arrs[0] == 0) {
			//关机成功
			$where['device_sn'] = $access_number;
			$res['state'] = 0;
			$this->Device_m->update($where,$res);

			//日入记录-关机
			$file  = './flowlogs/'.date('Y-m-d-H').'guanji.txt';
	        $content = date("Y/m/d h:i:sa").";备注=关机成功;设备号=".$access_number."\r\n";
	        file_put_contents($file, $content,FILE_APPEND);

			return array('code'=>3,'msg'=>'关机成功','access_number'=>$access_number);
		}else{
			//日入记录-关机
			$file  = './flowlogs/'.date('Y-m-d-H').'guanji.txt';
	        $content = date("Y/m/d h:i:sa").";备注=关机失败;设备号=".$access_number."\r\n";
	        file_put_contents($file, $content,FILE_APPEND);

			return array('code'=>4,'msg'=>'关机失败','access_number'=>$access_number);
		}
	}



/*+++++++++++++++以下是测试用++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

	public function kaiji()
	{
		// $aa = $this->Functionceshikai('1410331299946');
		// $aa = $this->Functionceshikai('1410333210026');
		// print_r($aa);die;
		$aa = $this->Device_m->select(array('state'=>0));
		foreach ($aa as $key => $value) {
			$aaa[] = $this->Functionceshikai($value['device_sn']);
		}
		echo "<pre>";
		print_r($aaa);die;
	}
	//开机
	public function Functionceshikai($access_number)
	{
		header("Content-type:text/html;charset=utf-8");
		$user_id="miaoyue";		//用户名
		$password="V934hoxZwdE2ZmWl";//密码
		$method = "disabledNumber";  //接口名-套餐使用量查询
		$orderTypeId = 20;
		$arr=array($access_number,$user_id,$password,$method,$orderTypeId);//加密数组，数组所需参数根据对应的接口文档
	    $key1 = "Jp2";
	    $key2 = "SMp";
	    $key3 = "4Ja";
	    $des=new DesUtils(); //加密工具类实例化
		
		$arrOrder=$des->naturalOrdering($arr); //加密数组元素排序
    	$sign = $des->strEnc($arrOrder,$key1,$key2,$key3);
	    $passwords = $des->strEnc($password,$key1,$key2,$key3);

		$urls = "http://api.e114.xin/m2m_ec/query.do?user_id=".$user_id."&access_number=".$access_number."&sign=".$sign."&passWord=".$passwords."&method=".$method."&orderTypeId=".$orderTypeId."";
		$reader = new XMLReader(); //实例化XMLReader
		$reader->open($urls); //获取xml
		$i=1;
		while ($reader->read()) {
		 if ($reader->nodeType == XMLReader::TEXT) { //判断node类型
		   $m = $i%3;
		   $Arr[] = $reader->value;
		   $i++;
		 }
		}
		
		if ($Arr[0] == 0) {
			//开机成功
			$where['device_sn'] = $access_number;
			$res['state'] = 1;
			$this->Device_m->update($where,$res);
			return array('code'=>1,'msg'=>'开机成功','access_number'=>$access_number);
		}else{
			return array('code'=>2,'msg'=>'开机失败','access_number'=>$access_number);
		}
		
	}


	public function guanji()
	{
		// $aa = $this->Functionceshiguanss('1410331299946');
		$aa = $this->Functionceshiguanss('1410333210026');
		print_r($aa);die;
	}
	//关机
	public function Functionceshiguanss($access_number)
	{
		header("Content-type:text/html;charset=utf-8");
		// header("Content-type:text/xml;charset=utf-8");
		//具体接口参数需参照自管理门户在线文档
		// $access_number=$access_number; //物联网卡号(149或10649号段)
		$user_id="miaoyue";		//用户名
		$password="V934hoxZwdE2ZmWl";//密码
		$method = "disabledNumber";  //接口名-套餐使用量查询
		$orderTypeId = 19;
		// $arr=array($access_number,$user_id,$password,$method,$acctCd,$orderTypeId);//加密数组，数组所需参数根据对应的接口文档
		$arr=array($access_number,$user_id,$password,$method,$orderTypeId);//加密数组，数组所需参数根据对应的接口文档
		//key值指密钥，由电信提供，每个客户都有对应的key值，key值平均分为三段如下：
	    $key1 = "Jp2";
	    $key2 = "SMp";
	    $key3 = "4Ja";
	    $des=new DesUtils(); //加密工具类实例化
		
		$arrOrder=$des->naturalOrdering($arr); //加密数组元素排序
    	$sign = $des->strEnc($arrOrder,$key1,$key2,$key3);
	    $passwords = $des->strEnc($password,$key1,$key2,$key3);

		$urls = "http://api.e114.xin/m2m_ec/query.do?user_id=".$user_id."&access_number=".$access_number."&sign=".$sign."&passWord=".$passwords."&method=".$method."&orderTypeId=".$orderTypeId."";
		// return $urls;
		// $ceshi = $this->RequestHttp($urls);
		// print_r($ceshi);die;

		$reader = new XMLReader(); //实例化XMLReader
		$reader->open($urls); //获取xml
		$i=1;
		while ($reader->read()) {
		 if ($reader->nodeType == XMLReader::TEXT) { //判断node类型
		   $m = $i%3;
		   $arrs[] = $reader->value;
		   $i++;
		 }
		}
		
		if ($arrs[0] == 0) {
			//关机成功
			$where['device_sn'] = $access_number;
			$res['state'] = 0;
			$this->Device_m->update($where,$res);
			return array('code'=>3,'msg'=>'关机成功','access_number'=>$access_number);
		}else{
			return array('code'=>4,'msg'=>'关机失败','access_number'=>$access_number);
		}
		// echo "<pre>";
		// print_r($arrs);die;
		
	}

	public function  chaxun(){
		header("Content-type:text/html;charset=utf-8");
		$access_number='1410331299946'; //物联网卡号(149或10649号段)
		// $access_number='1410333210026'; //物联网卡号(149或10649号段)
		$user_id="miaoyue";		//用户名
		$password="V934hoxZwdE2ZmWl";//密码
		$method = "queryPakage";  //接口名-套餐使用量查询
		$arr=array($access_number,$user_id,$password,$method);//加密数组，数组所需参数根据对应的接口文档

		//key值指密钥，由电信提供，每个客户都有对应的key值，key值平均分为三段如下：
	    $key1 = "Jp2";
	    $key2 = "SMp";
	    $key3 = "4Ja";
	    $des=new DesUtils(); //加密工具类实例化

	    $arrOrder=$des->naturalOrdering($arr); //加密数组元素排序
	    $sign = $des->strEnc($arrOrder,$key1,$key2,$key3);
	    $passwords = $des->strEnc($password,$key1,$key2,$key3);
	    $time = time();
		$url = "http://api.e114.xin/m2m_ec/query.do?user_id=".$user_id."&access_number=".$access_number."&sign=".$sign."&passWord=".$passwords."&method=queryPakage";
		// $xml = $this->RequestHttp($url);
		// print_r($xml);exit;
		$reader = new XMLReader(); //实例化XMLReader
		$reader->open($url); //获取xml
		$i=1;
		while ($reader->read()) {
		 if ($reader->nodeType == XMLReader::TEXT) { //判断node类型
		   $m = $i%3;
		   $arr[] = $reader->value;
		   $i++;
		 }
		}
		echo "<pre>";
		print_r($arr);die();
		$already = $arr[5];//已使用
		$left 	 = $arr[6];//剩余
		$total	 = $arr[7];//总量


		//停用的接口
	}
/*+++++++++++++++测试用end++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
	function curl_post($url, $post){
		$options = array(
		CURLOPT_RETURNTRANSFER =>true,
		CURLOPT_HEADER =>false,
		CURLOPT_POST =>true,
		CURLOPT_POSTFIELDS => $post,
		);
		$ch = curl_init($url);
		curl_setopt_array($ch, $options);
		$result = curl_exec($ch);
		curl_close($ch);
		return $result;
	}


}